import sys
import os
from datetime import datetime as dt
from MyData import MyData

# 读信息
if len(sys.argv) > 1:
    username = sys.argv[1]
else:
    username = "15399900725"

# 新建打卡类
dk = MyData(username)

# 检查是否有存档
if os.path.exists(username):
    dk.read_saved_info(username)

# 检查是否已打卡
has_recorded = dk.req_has_recorded()
if has_recorded['status'] == False:
    dk.req_subUser()
    print("%s 获取%s的五方科技access_token" % (dt.now(), username))
    has_recorded = dk.req_has_recorded()
    if has_recorded['status'] == False:
        print("%s %s没读取到打卡记录,可能是网络问题,也可能是服务器问题,请查询是否服务器改参数了" % (dt.now(), username))
        exit()
        
if dt.now().hour == 8:
    if has_recorded['morning'] == False:
        ret = dk.req_make_record(1)
        if "AccessToken" in ret['message']:
            os.remove("./username")
            print("AccessToken 好像已过期，删除存档，下一分钟再打卡")
        if ret != None:
            print("%s %s上班打卡:%s" % (dt.now(), username, ret['message']))
        else:
            print("%s %s上班打卡不成功" % (dt.now(), username))
    exit()

if dt.now().hour == 16:
    if has_recorded['evening'] == False:
        ret = dk.req_make_record(2)
        if "AccessToken" in ret['message']:
            os.remove("./username")
            print("AccessToken 好像已过期，删除存档，下一分钟再打卡")
        if ret != None:
            print("%s %s下班打卡:%s" % (dt.now(), username, ret['message']))
        else:
            print("%s %s上班打卡不成功" % (dt.now(), username))
    exit()